package com.winit.project.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.beans.LoginUser;
import com.winit.common.base.web.BaseController;
import com.winit.common.filter.UserInfoUtil;
import com.winit.common.util.Constants;
import com.winit.deploy.service.DeployService;
import com.winit.jumpServer.manager.PushRecordManager;
import com.winit.project.manager.ProjectManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by keel2008 on 16/9/27.
 */
@RestController
@RequestMapping("/project")
public class ProjectStatusController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ProjectStatusController.class);

    @Autowired
    private DeployService deployService;

    @Autowired
    private ProjectManager projectManager;

    /**
     * 标记提测
     * @param request
     * @return
     */
    @RequestMapping("/editionToTest")
    @ResponseBody
    public Map<String,Object> editionToTest(HttpServletRequest request){
        Map<String,Object> rtnMap = new HashMap<>();
        logger.info("标记提测开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null){
            logger.info("标记提测,获取用户信息为空");
            return this.noUserRtn();
        }

        String projectName = request.getParameter("projectName");
        String editionNum = request.getParameter("editionNum");
        String type = request.getParameter("type");
        Assert.hasText(projectName,"标记提测,工程名为空");
        Assert.hasText(editionNum,"标记提测,版本号为空");
        Assert.hasText(type,"标记提测,type为空");

        try {
            boolean flag = projectManager.editionToTest(projectName,editionNum,Boolean.parseBoolean(type),userInfo);
            if(flag){
                rtnMap.put("code","0");
            }else {
                rtnMap.put("code","1");
                rtnMap.put("desc","标记提测失败");
            }
        } catch (Exception e) {
            logger.error("标记提测异常.projectName="+projectName+",editionNum="+editionNum,e);
            rtnMap.put("code","1");
            rtnMap.put("desc","标记提测异常.e:"+e.getMessage());
        }

        return this.getRtnMap(rtnMap,userInfo,new JSONObject());
    }

    /**
     * 版本测试通过 or 不通过
     * @param request
     * @return
     */
    @RequestMapping("/updateTestStatus")
    @ResponseBody
    public Map<String,Object> updateTestStatus(HttpServletRequest request){
        Map<String,Object> rtnMap = new HashMap<>();
        logger.info("标记版本测试通过状态开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null){
            logger.info("标记版本测试通过状态,获取用户信息为空");
            return this.noUserRtn();
        }

        String projectName = request.getParameter("projectName");
        String editionNum = request.getParameter("editionNum");
        String flag = request.getParameter("flag");
        Assert.hasText(projectName,"标记版本测试通过状态,工程名为空");
        Assert.hasText(editionNum,"标记版本测试通过状态,版本号为空");
        Assert.hasText(flag,"标记版本测试通过状态,状态为空");

        try {
            String status = Boolean.parseBoolean(flag)? Constants.PROJECT_EDITION_TEST_STATUS_PASS:Constants.PROJECT_EDITION_TEST_STATUS_NOT_PASS;
            boolean f = deployService.updateProjectTestPassEdition(projectName,editionNum,status,userInfo);
            if(f){
                rtnMap.put("code","0");
            }else {
                rtnMap.put("code","1");
                rtnMap.put("desc","标记版本测试通过状态失败");
            }
        } catch (Exception e) {
            logger.error("标记版本测试通过状态异常.projectName="+projectName+",editionNum="+editionNum+",flag="+flag,e);
            rtnMap.put("code","1");
            rtnMap.put("desc","标记版本测试通过状态异常.e:"+e.getMessage());
        }

        return this.getRtnMap(rtnMap,userInfo,new JSONObject());
    }
}
